﻿package com.tudou.home.utils
{

    public class Binding extends Object
    {
        private static var list:Array = [];

        public function Binding()
        {
            return;
        }// end function

        public static function add(param1:String, param2:Object, param3:Function, param4:Array = null, param5:Boolean = false) : void
        {
            if (param4 == null)
            {
                param4 = [];
            }
            var _loc_6:Object = {thisObj:param2, fun:param3, params:param4, autoRemove:param5};
            var _loc_7:* = list[param1] as ;
            if (list[param1] as  == null)
            {
                _loc_7 = new Item();
                list[param1] = _loc_7;
            }
            if (_loc_7.fired)
            {
                doJob(_loc_6, _loc_7.fireParams);
                if (!param5)
                {
                    _loc_7.doList.push(_loc_6);
                }
            }
            else
            {
                _loc_7.doList.push(_loc_6);
            }
            return;
        }// end function

        public static function remove(param1:String, param2:Object, param3:Function = null) : void
        {
            var _loc_5:Object = null;
            var _loc_4:* = list[param1] as ;
            var _loc_6:* = param3 == null;
            if (_loc_4 == null)
            {
                return;
            }
            var _loc_7:* = _loc_4.doList.length - 1;
            while (_loc_7 >= 0)
            {
                
                _loc_5 = _loc_4.doList[_loc_7];
                if (_loc_5["thisObj"] == param2 && (_loc_6 || _loc_5["fun"] == param3))
                {
                    _loc_4.doList.splice(_loc_7, 1);
                }
                _loc_7 = _loc_7 - 1;
            }
            return;
        }// end function

        public static function fire(param1:String, param2:Array = null) : void
        {
            var _loc_4:Object = null;
            var _loc_3:* = list[param1] as ;
            if (_loc_3 == null)
            {
                _loc_3 = new Item();
                list[param1] = _loc_3;
            }
            _loc_3.fired = true;
            _loc_3.fireParams = param2;
            if (_loc_3.fireParams == null)
            {
                _loc_3.fireParams = [];
            }
            var _loc_5:* = _loc_3.doList.length - 1;
            while (_loc_5 >= 0)
            {
                
                _loc_4 = _loc_3.doList[_loc_5];
                doJob(_loc_4, _loc_3.fireParams);
                if (_loc_4["autoRemove"])
                {
                    _loc_3.doList.splice(_loc_5, 1);
                }
                _loc_5 = _loc_5 - 1;
            }
            return;
        }// end function

        private static function doJob(param1:Object, param2:Array) : void
        {
            var _loc_3:* = param1["thisObj"];
            var _loc_4:* = param1["fun"];
            var _loc_5:* = param1["params"];
            var _loc_6:* = param1["params"].concat(param2);
            _loc_4.apply(_loc_3, _loc_5.concat(param2));
            return;
        }// end function

    }
}

class Item extends Object
{
    private var doList:Array;
    private var fireParams:Array;
    private var fired:Boolean = false;

    function Item()
    {
        doList = [];
        fireParams = [];
        return;
    }// end function

}

